package lib;

import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-08-09
 * Time: 16:30
 */
public class Update {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("lmh041105666");

        Connection connection = (Connection) dataSource.getConnection();

        String sql = "update student set name = ? where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入你要修改的学生的id:");
        int id = scanner.nextInt();
        System.out.print("请输入修改后学生的姓名:");
        String name = scanner.nextLine();
        statement.setString(1,name);
        statement.setInt(2,id);

        int reult = statement.executeUpdate();

        statement.close();
        connection.close();
    }
}
